package com.water.camera.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;

import com.bumptech.glide.Glide;
import com.snap.base.base.BaseMainAct;
import com.snap.base.ktx.AdKTXKt;
import com.snap.base.ktx.ConfigKTXKt;
import com.snap.base.util.BaseUtil;
import com.snap.base.vm.DefaultVM;
import com.snap.base.vm.UserVM;
import com.snap.util.BarUtilKt;
import com.water.camera.BuildConfig;
import com.water.camera.R;
import com.water.camera.activity.camera.CameraActivity;
import com.water.camera.databinding.ActivityHomeBinding;
import com.water.camera.fragment.MarksFragment;
import com.water.camera.it.WaterMarkChangeListener;
import com.water.camera.kt.AdConfig;
import com.water.camera.kt.AdUtil;
import com.water.camera.kt.BuyAct;
import com.water.camera.kt.BuyDialog;
import com.water.camera.kt.ShareVM;
import com.water.camera.util.Read;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import me.jessyan.autosize.internal.CancelAdapt;

public class MainActivity extends BaseMainAct<ActivityHomeBinding, DefaultVM> implements CancelAdapt {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtilKt.setStatusBarLight(this, true);
    }

    @NonNull
    @Override
    public DefaultVM createVM() {
        return new DefaultVM();
    }

    @Override
    public void doDataBind() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_home;
    }

    @Override
    public void onCreate() {
        binding.importPicture.setOnClickListener(v -> {
            Read.toRead(this, new Runnable() {
                @Override
                public void run() {
                    Intent i = new Intent(MainActivity.this, PreviewPhotoAct.class);
                    startActivity(i);
                }
            });
        });
        binding.batchWatermarking.setOnClickListener(v -> {
            Read.toRead(this, new Runnable() {
                @Override
                public void run() {
                    Intent i = new Intent(MainActivity.this, BatchPhotoAct.class);
                    startActivity(i);
                }
            });
        });
        binding.jigsawReport.setOnClickListener(v -> {
            Intent i = new Intent(this, PinAct.class);
            startActivity(i);
        });
        addFragment(R.id.container, new MarksFragment().setWaterMarkChangeListener(new WaterMarkChangeListener() {
            @Override
            public void onWaterMarkChange(String name) {
                Intent i = new Intent(MainActivity.this, CameraActivity.class);
                i.putExtra("name", name);
                startActivity(i);
            }
        }));

        binding.capture.setOnClickListener(v -> {
            Intent i = new Intent(MainActivity.this, CameraActivity.class);
            startActivity(i);
        });
        binding.personage.setOnClickListener(v -> {
            Intent i = new Intent(this, MeActivity.class);
            startActivity(i);
        });

        AdUtil.Companion.showMainInsert(this);
    }

    public void addFragment(int id, Fragment fragment) {
        if (fragment == null) {
            return;
        }
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (fragment.isAdded()) {
                transaction.show(fragment);
            } else {
                transaction.add(id, fragment);
            }
            transaction.commitAllowingStateLoss();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!ShareVM.INSTANCE.isOutWithBuy()) {
            return;
        }
        ShareVM.INSTANCE.setOutWithBuy(false);
        // 判断是否显示 看广告免费使用
        if (!ConfigKTXKt.isEnableConfigWithKey(BaseUtil.INSTANCE, AdConfig.IS_SHOW_AD_FREE_USE)) {
            return;
        }
        new BuyDialog().showDialog(this, new Function0<Unit>() {
            @Override
            public Unit invoke() {
                Intent i = new Intent(MainActivity.this, BuyAct.class);
                startActivity(i);
                return null;
            }
        });
    }

    // 热启动回调 在这里判断是否需要显示插屏广告
    @Override
    public void onAdInsertFromHot() {
        super.onAdInsertFromHot();
        AdKTXKt.adInterstitial(BaseUtil.INSTANCE, this, AdConfig.AD_INSERT_HOT);
    }

    // 从其他页面回调 在这里判断是否需要显示插屏广告
    @Override
    public void onAdInsertFromOtherActResume() {
        AdKTXKt.adInterstitial(BaseUtil.INSTANCE, this, AdConfig.AD_INSERT_OTHER);
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (BuildConfig.DEBUG && keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onMainAfterTodo() {
        UserVM.INSTANCE.getUserHead().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Glide.with(binding.personage).load(s).into(binding.personage);
            }
        });
    }
}
